import { getUserInfoAPI, loginAPI } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: {},
    userId: 0,
    // 主动介入token 登录起始时间
    likedTime: 0
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    removeToken(state) {
      state.token = ''
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeUserInfo(state) {
      state.userInfo = {}
    },
    setUserId(state, userId) {
      state.userId = userId
    },
    setLikedTime(state, time) {
      state.likedTime = time
    }
  },
  actions: {
    async loginAction({ commit }, data) {
      const res = await loginAPI(data)
      commit('setToken', res.data.token)
      commit('setUserId', res.data.userId)
      commit('setLikedTime', Date.now())
    },
    async getUserInfoAction({ commit }, id) {
      const { data: res } = await getUserInfoAPI(id)
      console.log(res)
      commit('setUserInfo', res)
    },
    logout({ commit }) {
      commit('removeToken')
      commit('removeUserInfo')
    }
  }

}
